package com.flyco.modules.cms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.cms.entity.CmsContentV1;
import com.flyco.modules.cms.entity.CmsContentV1History;
import com.flyco.modules.cms.entity.CmsContentV1Preview;
import com.flyco.modules.cms.service.ICmsContentV1HistoryService;
import com.flyco.modules.cms.service.ICmsContentV1PreviewService;
import com.flyco.modules.cms.service.ICmsContentV1Service;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 商城内容管理
 * @Author: flyco
 * @Date: 2020-02-27
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "商城内容管理")
@RestController
@RequestMapping("/f/cms/cmsContentV1")
public class CmsContentV1Controller extends BaseController<CmsContentV1, ICmsContentV1Service> {
    @Autowired
    private ICmsContentV1Service cmsContentV1Service;
    @Autowired
    private ICmsContentV1HistoryService cmsContentV1HistoryService;
    @Autowired
    private ICmsContentV1PreviewService cmsContentV1PreviewService;

    /**
     * 分页列表查询
     *
     * @param cmsContentV1
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "商城内容管理-分页列表查询")
    @ApiOperation(value = "商城内容管理-分页列表查询", notes = "商城内容管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CmsContentV1 cmsContentV1,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<CmsContentV1> queryWrapper = QueryGenerator.initQueryWrapper(cmsContentV1, req.getParameterMap());
        Page<CmsContentV1> page = new Page<CmsContentV1>(pageNo, pageSize);
        IPage<CmsContentV1> pageList = cmsContentV1Service.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param cmsContentV1
     * @return
     */
    @AutoLog(value = "商城内容管理-添加")
    @ApiOperation(value = "商城内容管理-添加", notes = "商城内容管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CmsContentV1 cmsContentV1) {
        cmsContentV1.setCreateBy("");
        cmsContentV1.setCreateTime(null);
        cmsContentV1.setUpdateBy("");
        cmsContentV1.setUpdateTime(null);
        cmsContentV1Service.save(cmsContentV1);
        saveContentV1History(cmsContentV1);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cmsContentV1
     * @return
     */
    @AutoLog(value = "商城内容管理-编辑")
    @ApiOperation(value = "商城内容管理-编辑", notes = "商城内容管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CmsContentV1 cmsContentV1) {
        cmsContentV1Service.updateById(cmsContentV1);
        saveContentV1History(cmsContentV1);
        return Result.ok("编辑成功!");
    }

    private void saveContentV1History(CmsContentV1 cmsContentV1) {
        CmsContentV1History history = new CmsContentV1History();
        BeanUtils.copyProperties(cmsContentV1, history);
        history.setContentId(cmsContentV1.getId());
        history.setId(null);
        history.setCreateBy(null);
        history.setCreateTime(null);
        cmsContentV1HistoryService.save(history);
    }

    /**
     * 编辑content
     *
     * @param cmsContentV1
     * @return
     */
    @AutoLog(value = "商城内容管理-编辑content")
    @ApiOperation(value = "商城内容管理-编辑content", notes = "商城内容管理-编辑content")
    @PutMapping(value = "/editContentById")
    public Result<?> editContentById(@RequestBody CmsContentV1 cmsContentV1) {
        CmsContentV1 dbContentV1 = cmsContentV1Service.getById(cmsContentV1.getId());
        CmsContentV1 editContent = new CmsContentV1();
        editContent.setContent(cmsContentV1.getContent());
        editContent.setLoadKeywords(cmsContentV1.getLoadKeywords());
        editContent.setId(cmsContentV1.getId());
        cmsContentV1Service.updateById(editContent);
        dbContentV1.setContent(cmsContentV1.getContent());
        dbContentV1.setLoadKeywords(cmsContentV1.getLoadKeywords());
        saveContentV1History(dbContentV1);
        return Result.ok("编辑成功!");
    }


    /**
     * 预览content
     *
     * @param cmsContentV1
     * @return
     */
    @AutoLog(value = "商城内容管理-预览content")
    @ApiOperation(value = "商城内容管理-预览content", notes = "商城内容管理-预览content")
    @PutMapping(value = "/previewContentById")
    public Result<?> previewContentById(@RequestBody CmsContentV1 cmsContentV1) {
        CmsContentV1 dbContentV1 = cmsContentV1Service.getById(cmsContentV1.getId());
        CmsContentV1Preview previewContent = new CmsContentV1Preview();
        BeanUtils.copyProperties(dbContentV1, previewContent);
        previewContent.setContent(cmsContentV1.getContent());
        previewContent.setLoadKeywords(cmsContentV1.getLoadKeywords());
        cmsContentV1PreviewService.saveOrUpdate(previewContent);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商城内容管理-通过id删除")
    @ApiOperation(value = "商城内容管理-通过id删除", notes = "商城内容管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        cmsContentV1Service.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商城内容管理-批量删除")
    @ApiOperation(value = "商城内容管理-批量删除", notes = "商城内容管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.cmsContentV1Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商城内容管理-通过id查询")
    @ApiOperation(value = "商城内容管理-通过id查询", notes = "商城内容管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CmsContentV1 cmsContentV1 = cmsContentV1Service.getById(id);
        return Result.ok(cmsContentV1);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param cmsContentV1
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CmsContentV1 cmsContentV1) {
        return super.exportXls(request, cmsContentV1, CmsContentV1.class, "商城内容管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CmsContentV1.class);
    }

}
